package com.heer.gymnasium.module.main.views;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.util.Log;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.event.BaseEvent;
import com.heer.gymnasium.module.coach.views.CoachDetailActivity;
import com.heer.gymnasium.module.store.views.StoreActivity;
import com.zhihuiyun.library.adapter.MainFragmentAdapter;
import com.zhihuiyun.library.tab.NavigationTabBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.heer.gymnasium.event.EventConstant.MINE_TO_ORDER;
import static com.heer.gymnasium.event.EventConstant.USER_NEW_MSG;

/**
 * Created by yan on 2018/1/11.
 */

public class MainActivity extends BaseActivity {

    private final int RES_ID_CENTER = R.id.layout_main_center_fl;

    @BindView(R.id.layout_main_vp)
    ViewPager viewPager;

    @BindView(R.id.layout_main_ntb)
    NavigationTabBar navigationTabBar;
    private FellowListFragment fellowListFragment;

    private List<NavigationTabBar.Model> models;
    private int index = 0;

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.layout_main);
        EventBus.getDefault().isRegistered(this);
    }

    @Override
    protected void initView() {
        super.initView();
        initTabBar();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void eventBus(BaseEvent event) {
        if (TextUtils.equals(event.getId(), MINE_TO_ORDER)) {

        } else if (TextUtils.equals(event.getId(), USER_NEW_MSG)) {

        }
    }

    /**
     * 初始化tabBar
     */
    private void initTabBar() {
        models = new ArrayList<>();
        models.add(new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.ic_main_home_normal))
                .selectedIcon(getResources().getDrawable(R.drawable.ic_main_home_select))
                .title("首页")
                .build());
        models.add(new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.ic_main_admission_normal))
                .selectedIcon(getResources().getDrawable(R.drawable.ic_main_admission_select))
                .title("入场")
                .build());
        models.add(new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.ic_main_admission_normal))
                .selectedIcon(getResources().getDrawable(R.drawable.ic_main_admission_normal))
                .title("约")
                .build());
        models.add(new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.ic_main_find_normal))
                .selectedIcon(getResources().getDrawable(R.drawable.ic_main_find_select))
                .title("发现")
                .build());
        models.add(new NavigationTabBar.Model.Builder(getResources().getDrawable(R.drawable.ic_main_my_normal))
                .selectedIcon(getResources().getDrawable(R.drawable.ic_main_my_select))
                .title("我的")
                .build());

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new AdmissionFragment());
        fragments.add(fellowListFragment = new FellowListFragment());
        fragments.add(new FindFragment());
        fragments.add(new MyFragment());

        viewPager.setAdapter(new MainFragmentAdapter(this.getSupportFragmentManager(), fragments));
        viewPager.setOffscreenPageLimit(5);
        navigationTabBar.setModels(models);
        index = getIntent().getIntExtra(EXTRA_DATA, 0);
        navigationTabBar.setViewPager(viewPager, index);
        navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                navigationTabBar.getModels().get(position).hideBadge();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }

    @OnClick(RES_ID_CENTER)
    public void onCenterClicked() {
        setCurrentItem(this, 2);
        if (fellowListFragment != null) {
            fellowListFragment.reset();
        }
    }

    public static void setCurrentItem(Context context, int index) {
        if (context instanceof MainActivity) {
            MainActivity mainActivity = (MainActivity) context;
            mainActivity.viewPager.setCurrentItem(index);
        }
    }

    public static void startActivity(Context context) {
        Intent mIntent = new Intent(context, MainActivity.class);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(mIntent);
    }

    public static void startActivity(Context context, int currentItem) {
        Intent mIntent = new Intent(context, MainActivity.class);
        mIntent.putExtra(EXTRA_DATA, currentItem);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(mIntent);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                if (data.getExtras() != null) {
                    Bundle bundle = data.getExtras();
                    String result = bundle.getString("result");
                    if (result.contains(IConstants.BASRURL + "/dist/coachDetail?id=")) {
                        CoachDetailActivity.startActivity(this, result.substring(result.indexOf("=") + 1, result.length()));
                    } else if (result.contains(IConstants.BASRURL + "/dist/shopDetail?id=")) {
                        StoreActivity.startActivity(this, result.substring(result.indexOf("=") + 1, result.length()));
                    }
                }
            }
        }
    }
}
